﻿module FQuant

//  Recursive reduce function
//  func  : The function to apply
//  value : The terminating value
//  list  : The list to apply the value on.
let rec reduce func value list =
  match list with 
  | [] -> value
  | h::t -> func h (reduce func value t)
  
let mult a b = a * b 
let count list = reduce (fun a b -> 1 + 1) 0 list
let sum list = reduce (fun a b -> a + b) 0 list
let sum_squares list = reduce (fun a b -> a + b), 0, list |> List.map(fun x -> x * x)
let product list = reduce (fun a b -> a + b), 1, list

let mean timeSeries = 
  sum timeSeries / timeSeries.Length

let geometric_mean timeSeries =
  // Return the geometric mean of a time series: 
  let productOfSeries = reduce mult 1 timeSeries 
  productOfSeries * (1 / timeSeries.Length)

//let harmonic_mean timeSeries =
//  // Return the harmonic mean of a time series: 
//  let inverse x = 1 / x 
//  let reciprocals = timeSeries |> List.map(inverse)
//  let sumOfReciprocals = reduce sum 0 reciprocals
//  timeSeries.Length / sumOfReciprocals
